define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'zhongzhuan/ec/ecmapfield/index' + location.search,
                    add_url: 'zhongzhuan/ec/ecmapfield/add',
                    edit_url: 'zhongzhuan/ec/ecmapfield/edit',
                    del_url: 'zhongzhuan/ec/ecmapfield/del',
                    multi_url: 'zhongzhuan/ec/ecmapfield/multi',
                    import_url: 'zhongzhuan/ec/ecmapfield/import',
                    table: 'ec_map_fields',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'fieldId', title: __('Fieldid'), operate: 'LIKE'},
                        {field: 'category', title: __('Category'), operate: 'LIKE'},
                        {field: 'fieldName', title: __('Fieldname'), operate: 'LIKE'},
                        {field: 'fieldSort', title: __('Fieldsort'), operate: 'LIKE'},
                        {field: 'filedType', title: __('Filedtype'), operate: 'LIKE'},
                        {field: 'fieldGroupId', title: __('Fieldgroupid'), operate: 'LIKE'},
                        {field: 'fieldGroupName', title: __('Fieldgroupname'), operate: 'LIKE'},
                        {field: 'fieldGroupSort', title: __('Fieldgroupsort'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                        }
                    ]
                ]
            });

            $(document).on("click",'.btn-auto-update', function () {
                console.log(111);
                layer.confirm('确认要自动更新EC自定义字段到本系统？', {
                    btn: ['确认'] //可以无限个按钮
                  }, function(index, layero){
                     $.ajax({
                         url:'/api/ec.eccompany/getFieldMapping',
                         type:'POST',
                         success:function(res){
                             layer.msg(res.msg)
                         }
                     })
                  });
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
